<?php
//Include the package

class Discussion_controller extends CI_Controller
{
    
    //Ham dung
    public function construct()
    {
        parent::__construct;
        $this->load->helper("url");
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
    }

    //Ham goi ham the hien tu file views/discussion_view/discussion_view.php
    public function view()
    {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        if($this->session->userdata('validated') != null) {
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            $add_data['current_course'] = $this->cache->apc->get('current_course', $add_data['user']['ID']);
            $add_data['course_id'] = $add_data['current_course']['course_id'];
            $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);
            //Lenh goi ham load du lieu trong model        
            $this->load->Model("Discussion_model");
            
            $add_data['ques_inf'] = $this->Discussion_model->listall_ques_by_id($add_data['current_course']['course_id']);
            
            
            //Lenh goi phan view
            $this->load->view("discussion_view/discussion_view", $add_data);
        } else {
            ?>
                <script type="text/javascript">
                    alert("Bạn chưa đăng nhập!!");
                </script>
            <?php
        }
    }

    //Ham goi file views/discussion_view/TrangSuaCauHoi.php
    public function edit_ques()
    {
        $this->load->view("discussion_view/ques/edit_ques.php");
    }

    public function show_ques($id = '')
    {
        $this->load->Model("Discussion_model");
        $add_data['ques'] = $this->Discussion_model->ques_get($id);
        $add_data['content'] = $this->Discussion_model->ans_get_content($id,
            "content");
        $add_data['id'] = $this->Discussion_model->ans_get_content($id, "id");
        $add_data['time'] = $this->Discussion_model->ans_get_content($id, "time");
        $add_data['email'] = $this->Discussion_model->ans_get_content($id, "email");
        $add_data['name'] = $this->Discussion_model->ans_get_content($id, "name");
        
        $this->load->library('session');
        $user_id = $this->session->userdata['ID'];
        $add_data['user'] = $this->session->all_userdata();

        //Trang luu du lieu vao bo dem
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $add_data['current_course'] = $this->cache->apc->get('current_course', $add_data['user']['ID']);
        $add_data['course_id'] = $add_data['current_course']['course_id'];
        $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);
        // Save into the cache for 500 minutes
        $this->cache->apc->save('disc_ques', $user_id, $add_data['ques'], 30000);


        $this->load->view("discussion_view/ques/show_ques", $add_data);
    }

    //Ham goi file views/discussion_view/TrangTraLoi.php
    public function ans_ques($id = '')
    {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        $this->load->helper("url");
        
        //Gọi ckeditor
        $this->load->library('ckeditor');
        $this->load->library('ckfinder');
        //Thư mục asset chứa ckeditor và ckfinder
        $this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
        //Thiết lập các tool icon ckeditor
        $this->ckeditor->config['toolbar'] = array(array(
                'Source',
                '-',
                'Bold',
                'Italic',
                'Underline',
                '-',
                'Cut',
                'Copy',
                'Paste',
                'PasteText',
                'PasteFromWord',
                '-',
                'Undo',
                'Redo',
                '-',
                'NumberedList',
                'BulletedList',
                'Find',
                'Replace',
                '-',
                'SelectAll',
                '-',
                'SpellChecker',
                'Scayt',
                'Link',
                'Unlink',
                'Anchor',
                'Image',
                'Flash',
                'Table',
                'PageBreak',
                'Styles',
                'Format',
                'Font',
                'FontSize',
                'TextColor',
                'BGColor',
                'Code'));
        // Thiết lập ngôn ngữ hiện thử en => english, vi => Việt Nam , fr => Pháp
        $this->ckeditor->config['language'] = 'vi';
        $this->ckeditor->config['width'] = '730px';
        $this->ckeditor->config['height'] = '400px';
        //Thêm ckfinder vào
        $this->ckfinder->SetupCKEditor($this->ckeditor, '../../asset/ckfinder/');
        
        $this->load->library('session');
        $user_id = $this->session->userdata['ID'];
        //Goi bo nho dem
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $add_data['current_course'] = $this->cache->apc->get('current_course', $add_data['user']['ID']);
        $add_data['course_id'] = $add_data['current_course']['course_id'];
        $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);
        //Bien $test_call_from_view dung de kiem tra xem trang web
        $add_data['ques'] = $this->cache->apc->get('disc_ques', $add_data['user']['ID']);

        $this->load->view("discussion_view/ans/ans_ques", $add_data);

    }

    //Xem thu cau hoi truoc khi ghi lai
    public function review_ans($id_Ques = '')
    {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        $add_data['txt_answer'] = $_POST['txt_get_ckeditor'];
        
        //Goi bo nho dem
        $this->load->library('session');
        $user_id = $this->session->userdata['ID'];
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $add_data['ques'] = $this->cache->apc->get('disc_ques', $user_id);
        

        //Trang luu du lieu vao bo dem
        //Saving to the cache
        // Save into the cache for 500 minutes
        $this->cache->apc->save('txt_new_ans', $user_id, $add_data['txt_answer'], 30000);
        $add_data['current_course'] = $this->cache->apc->get('current_course', $add_data['user']['ID']);
        $add_data['course_id'] = $add_data['current_course']['course_id'];
        $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);


        $this->load->view('discussion_view/ans/review_ans', $add_data);
    }

    //Ghi cau hoi vao co so du lieu
    public function record_ans()
    {
        //Goi du lieu tu bo nho dem
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $add_data['current_course'] = $this->cache->apc->get('current_course', $add_data['user']['ID']);
        $add_data['course_id'] = $add_data['current_course']['course_id'];
        $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);
        
        $data_ans['txt_ans'] = $this->cache->apc->get('txt_new_ans', $add_data['user']['ID']);
        
        $data_ans['course_id'] = $add_data['current_course']['course_id'];
        
        $disc_data['ques'] = $this->cache->apc->get('disc_ques', $add_data['user']['ID']);
        
        $data_ans['id_ques'] = $disc_data['ques']['disc_ques_id'];
        
        $data_ans['ques_quantity_ans'] = $disc_data['ques']['disc_ques_quantity_ans'];

        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $data_ans['ans_time'] = date("Y-m-d H:i:s");
        //echo $data_ans['answer_time'];
        
        $data_ans['user'] = $this->session->all_userdata();

        $this->load->Model("Discussion_model");    
        $this->Discussion_model->insert_ans($data_ans);
        
        $ans_quan = $this->session->userdata['ans_quan'] + 1; 
        //update the ans_quan in session data
        $this->session->unset_userdata('ans_quan');
        $new_array = array('ans_quan'=>$ans_quan);
        $this->session->set_userdata($new_array);    
        
        $add_data['ques_inf'] = $this->Discussion_model->listall_ques_by_id($data_ans['course_id']);
        
        $this->load->view('discussion_view/discussion_view', $add_data);
        
        //$this->load->view("discussion_view/show_ques", data_ans);
    }
    
    //Dat cau hoi
    public function ask_a_ques(){     
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        
        $this->load->helper('url');
        //Gọi ckeditor
        $this->load->library('ckeditor');
        $this->load->library('ckfinder');
        //Thư mục asset chứa ckeditor và ckfinder
        $this->ckeditor->basePath = base_url() . 'asset/ckeditor/';
        //Thiết lập các tool icon ckeditor
        $this->ckeditor->config['toolbar'] = array(array(
                'Source',
                '-',
                'Bold',
                'Italic',
                'Underline',
                '-',
                'Cut',
                'Copy',
                'Paste',
                'PasteText',
                'PasteFromWord',
                '-',
                'Undo',
                'Redo',
                '-',
                'NumberedList',
                'BulletedList',
                'Find',
                'Replace',
                '-',
                'SelectAll',
                '-',
                'SpellChecker',
                'Scayt',
                'Link',
                'Unlink',
                'Anchor',
                'Image',
                'Flash',
                'Table',
                'PageBreak',
                'Styles',
                'Format',
                'Font',
                'FontSize',
                'TextColor',
                'BGColor',
                'Code'));
        // Thiết lập ngôn ngữ hiện thử en => english, vi => Việt Nam , fr => Pháp
        $this->ckeditor->config['language'] = 'vi';
        $this->ckeditor->config['width'] = '740px';
        $this->ckeditor->config['height'] = '400px';
        //Thêm ckfinder vào
        $this->ckfinder->SetupCKEditor($this->ckeditor, '../../asset/ckfinder/');
        
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        $add_data['current_course'] = $this->cache->apc->get('current_course', $add_data['user']['ID']);
        $add_data['course_id'] = $add_data['current_course']['course_id'];
        $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);
        
        $this->load->view('discussion_view/ques/ask_a_ques', $add_data);
    }
    
    //Trang xem thu cau hoi
    public function review_ques(){
        
        $add_data['title_of_ques'] = $_POST['title_of_ques'];
        $add_data['txt_ques'] = $_POST['txt_get_ckeditor'];
        
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
             

        //Trang luu du lieu vao bo dem
        $this->load->library('session');   
        $user_id = $this->session->userdata['ID'];
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));       
        
        //Saving to the cache
        // Save into the cache for 500 minutes
        $this->cache->apc->save('title_of_new_ques', $user_id, $add_data['title_of_ques'], 30000);
               
        //Saving to the cache
        //Save into the cache for 500 minutes
        $this->cache->apc->save('txt_of_new_ques', $user_id, $add_data['txt_ques'], 30000);
        $add_data['ques_asker_name'] = $this->session->userdata('USERNAME');
        
        $add_data['current_course'] = $this->cache->apc->get('current_course', $add_data['user']['ID']);
        $add_data['course_id'] = $add_data['current_course']['course_id'];
        $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);

        $this->load->view('discussion_view/ques/review_ques', $add_data);
    }
    
    //Ham goi model ghi cau hoi moi vao csdl
    public function record_ques(){
        $this->load->library('session');   
        $add_data['user'] = $this->session->all_userdata();
        //Goi du lieu tu bo nho dem
        $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
        
        $add_data['current_course'] = $this->cache->apc->get('current_course', $add_data['user']['ID']);
        $add_data['course_id'] = $add_data['current_course']['course_id'];
        $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);
        
        $data_ques['title_of_ques'] = $this->cache->apc->get('title_of_new_ques', $add_data['user']['ID']);
        $data_ques['txt_ques'] = $this->cache->apc->get('txt_of_new_ques', $add_data['user']['ID']);
        
        
        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $data_ques['ques_time'] = date("Y-m-d H:i:s");

        $data_ques['name'] = $this->session->userdata['USERNAME'];
        $data_ques['email'] = $this->session->userdata['EMAIL'];
        $data_ques['ques_quan'] = $this->session->userdata['ques_quan'] + 1;
        $data_ques['ID'] = $this->session->userdata['ID'];
        $data_ques['course_id'] = $add_data['current_course']['course_id'];
        $this->load->Model("Discussion_model");
        $this->Discussion_model->insert_ques($data_ques);
        
        //update the ques_quan in session data
        $this->session->unset_userdata('ques_quan');
        $new_array = array('ques_quan'=>$data_ques['ques_quan']);
        $this->session->set_userdata($new_array);
        
        $this->load->Model("Discussion_model");
        $add_data['ques_inf'] = $this->Discussion_model->listall_ques_by_id($add_data['current_course']['course_id']);
        
        

        //Lenh goi phan view      
        $this->load->view('discussion_view/discussion_view',$add_data);
    }

}
?>
